package cn.rxyb.boot.rabbitmq.config;


import cn.rxyb.boot.rabbitmq.enums.QueueEnums;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YJH
 * @version 1.0延迟队列
 * @description
 * @date 2020/6/29 17:30
 */
@Configuration
public class RabbitConfig {


    /**
     *      * 延时队列交换机
     *      * 注意这里的交换机类型：CustomExchange
     *      *
     *      * @return
     *      
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnums.R_LOG_RECORD1.getExchange(), "x-delayed-message", true, false, args);
    }

    /**
     *      * 延时队列
     *      *
     *      * @return
     *      
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(QueueEnums.R_LOG_RECORD1.getQueue());
    }

    /**
     *      * 给延时队列绑定交换机
     *      *
     *      * @return
     *      
     */
    @Bean
    public Binding cfgDelayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(QueueEnums.R_LOG_RECORD1.getRouting()).noargs();
    }
}
